package org.dromara.web.websocket;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.web.websocket.request.Request;
import org.dromara.web.websocket.request.SubscribeRequest;
import org.dromara.web.websocket.request.UserLoginRequest;
import org.dromara.websocket.NettyWebsocketServer;
import org.dromara.websocket.connection.Connection;
import org.dromara.websocket.connection.ConnectionListener;
import org.dromara.websocket.message.MessageListener;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * Create Date      2023-09-28
 * Email:
 * Version          1.0.0
 * <p>
 * Description:
 */
public class WebsocketManager implements InitializingBean, ConnectionListener, MessageListener {

    @Autowired
    SocketProperties socketProperties;

    public NettyWebsocketServer server;


    @Autowired
    SubscribeConnectionHolder subscribeConnectionHolder;
    @Autowired
    UserConnectionHolder userConnectionHolder;

    @Override
    public void afterPropertiesSet() throws Exception {
        server = NettyWebsocketServer.builder()
                .connectionListener(this)
                .messageListener(this)
                .contextPath(socketProperties.getContextPath())
                .build();


        // 开启socket服务
        server.start(socketProperties.getPort());
    }


    /**
     * netty 回调    有消息进来时
     *
     * @param connection
     * @param content
     */
    @Override
    public void onMessage(Connection connection, String content) {
        Request request = JSONObject.parseObject(content, Request.class);
        if (Request.CMD_SUBSCRIBE.equals(request.getCommand())) {
            SubscribeRequest subscribeRequest = JsonUtils.parseObject(request.getContent(), SubscribeRequest.class);
            subscribeConnectionHolder.onSubscribe(connection, subscribeRequest);
            String[] strings = subscribeRequest.getTopic().split("_");
//            if (strings.length > 3) {
//                twelvedataWebsocketClient.subscribeMarketSymbol(strings[2], strings[3]);
//            }
        } else if (Request.CMD_UNSUBSCRIBE.equals(request.getCommand())) {
            subscribeConnectionHolder.deSubscribe(connection, JsonUtils.parseObject(request.getContent(), SubscribeRequest.class));
        } else if (Request.CMD_LOGIN.equals(request.getCommand())) {
            userConnectionHolder.onBind(connection, JsonUtils.parseObject(request.getContent(), UserLoginRequest.class));
        }
    }

    /**
     * 有连接进来时
     *
     * @param connection
     */
    @Override
    public void arrived(Connection connection) {

    }

    /**
     * 连接断开时
     *
     * @param connection
     */
    @Override
    public void disconnected(Connection connection) {
        subscribeConnectionHolder.onConnectionRemoved(connection);
    }
}
